package com.project.market.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.project.market.service.AdminRegionService;
import com.project.market.service.impl.AdminRegionServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/region/*")
public class AdminRegionController extends HttpServlet {

    AdminRegionService adminRegionService = new AdminRegionServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/region/", "");
        if ("list".equals(op)) {
            list(req,resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Object> list = adminRegionService.list();

        Object ok = ResponseUtil.okList(list);
        String s = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(s);
    }
}
